package com.shenhaofeng.app.http.request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 数据以键值对的的数据形式,这是最常用的数据格式
 * Created by Administrator on 2016/10/13.
 */
public class FormBody extends RequestBody {

    private final List<String> encodedNames = new ArrayList<>();

    private final List<String> encodedValues = new ArrayList<>();

    private static final MediaType CONTENT_TYPE = MediaType.create("application/x-www-form-urlencoded");


    protected FormBody(List<String> encodedNames, List<String> encodedValues) {
        this.encodedNames.addAll(encodedNames);
        this.encodedValues.addAll(encodedValues);
    }


    public List<String> getEncodedNames() {
        return encodedNames;
    }


    public String getEncodedName(int index) {
        return encodedNames.get(index);
    }


    public List<String> getEncodedValues() {
        return encodedValues;
    }


    public String getEncodedValue(int index) {
        return encodedValues.get(index);
    }


    public int size() {
        return encodedNames.size();
    }


    @Override
    public MediaType contentType() {
        return CONTENT_TYPE;
    }


    public static class Builder {


        private final List<String> names = new ArrayList<>();

        private final List<String> values = new ArrayList<>();


        public Builder add(String name, String value) {
            names.add(name);
            values.add(value);
            return this;
        }

//        public Builder addEncoded(String name, String value) {
//            names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true));
//            values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, true));
//            return this;
//        }


        public FormBody build() {
            return new FormBody(names, values);
        }

    }


}
